package RecursiveSearcher;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LC46
 * @Description 全排列
 * @Author ZJX
 * @Date 2024/9/17 11:58
 * @Version 1.0
 */
//    https://leetcode.cn/problems/permutations/
public class LC46 {
    // 将 result 和 tempList 作为全局变量
    private List<List<Integer>> result = new ArrayList<>();
    private List<Integer> tempList = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        permuteHelper(nums);  // 递归调用
        return result;
    }

    // 辅助递归函数
    private  void permuteHelper(int[] nums) {
        if (tempList.size() == nums.length) {
            result.add(new ArrayList<>(tempList));
            return;
        } else {
            for (int i = 0; i < nums.length; i++) {
                if (tempList.contains(nums[i])) {
                    continue;
                }
                tempList.add(nums[i]); // 添加
                permuteHelper(nums);
                tempList.remove(tempList.size() - 1);
            }
        }
    }

    public static void main(String[] args) {
       LC46 solution = new LC46();
        int[] nums = {1, 2, 3};  // 测试数组
        List<List<Integer>> permutations = solution.permute(nums);  // 生成全排列
        System.out.println(permutations);  // 打印结果
    }
}
